package com.ea.admin.controller.superAdmin;

import com.ea.admin.permission.PermissionCode;
import com.ea.admin.pojo.Enum.Permission;
import com.ea.admin.pojo.param.account.*;
import com.ea.admin.pojo.po.AdminAccount;
import com.ea.admin.pojo.vo.account.GetAdminListVo;
import com.ea.admin.pojo.vo.account.GetAdminRoleVo;
import com.ea.admin.pojo.vo.account.GetOperateLogVo;
import com.ea.admin.service.AccountService;
import com.ea.admin.utils.NowUserGetter;
import com.ea.common.log.LogAnnotation;
import com.ea.common.result.BizException;
import com.ea.common.result.ErrorCode;
import com.ea.common.result.Result;
import com.ea.common.utils.ParamUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

@RestController
@Api(tags = "超级管理-管理员管理")
@RequestMapping("/admin/super/account")
public class SuperAccountController {
    @Resource
    AccountService accountService;
    @Resource
    NowUserGetter nowUserGetter;

    @Transactional
    @Operation(summary = "获得管理员角色")
    @PostMapping("/getAdminRole")
    @LogAnnotation(module = "AdminController", operation = "获得管理员列表")
    @Permission(permission_code = PermissionCode.SUPER_MANAGE)
    public Result<GetAdminRoleVo> getAdminRole() {
        GetAdminRoleVo result = accountService.getAdminRole();
        return Result.success(result);
    }

    @Transactional
    @Operation(summary = "获得管理员列表")
    @ApiImplicitParam(dataType = "GetAdminListParam", name = "getAdminListParam", value = "获得管理员列表", readOnly = true, required = true)
    @PostMapping("/getAdminList")
    @LogAnnotation(module = "AdminController", operation = "获得管理员列表")
    @Permission(permission_code = PermissionCode.SUPER_MANAGE)
    public Result<GetAdminListVo> getAdminList(@RequestBody @Valid GetAdminListParam getAdminListParam) {
        GetAdminListVo result = accountService.getAdminList(
                nowUserGetter.getNowUser().getAccountId(),
                getAdminListParam.getPage(),
                getAdminListParam.getPageSize(),
                getAdminListParam.getUsername(),
                getAdminListParam.getName(),
                getAdminListParam.getPhoneNumber(),
                ParamUtil.parseIdList(getAdminListParam.getCompanyId()),
                ParamUtil.parseIdList(getAdminListParam.getDeptId()),
                getAdminListParam.getAdminRoleId()
        );
        return Result.success(result);
    }

    @Transactional
    @Operation(summary = "添加管理员")
    @ApiImplicitParam(dataType = "AddAdminParam", name = "addAdminParam", value = "添加管理员", readOnly = true, required = true)
    @PostMapping("/addAdmin")
    @LogAnnotation(module = "AdminController", operation = "添加管理员")
    @Permission(permission_code = PermissionCode.SUPER_ADD_ADMIN,
            content = "添加{adminRoleName}“{username}”，姓名为：“{name}”，电话号码为：“{phoneNumber}”")
    public Result<String> addAdmin(@RequestBody @Valid AddAdminParam addAdminParam) {
        // 1. 检查传入的password和rePassword是否相同，不相同的话，则报“密码和确认密码不一致”错误
        if (!addAdminParam.getPassword().equals(addAdminParam.getRePassword())) {
            throw new BizException(ErrorCode.USER_REGISTER_ERROR_PASSWORD_NOT_EQUALS);
        }
        AdminAccount data=accountService.addAdmin(
                addAdminParam.getUsername(),
                addAdminParam.getPassword(),
                addAdminParam.getRePassword(),
                addAdminParam.getName(),
                addAdminParam.getPhoneNumber(),
                ParamUtil.parseId(addAdminParam.getAdminRoleId()));
        return Result.success(String.valueOf(data.getAccountId()));
    }

    @Transactional
    @Operation(summary = "修改管理员信息")
    @ApiImplicitParam(dataType = "EditAdminParam", name = "editAdminParam", value = "修改管理员信息", readOnly = true, required = true)
    @PostMapping("/editAdmin")
    @LogAnnotation(module = "AdminController", operation = "修改管理员信息")
    @Permission(permission_code = PermissionCode.EDIT_ADMIN,
            content = "修改管理员“{username}”的信息")
    public Result editAdmin(@RequestBody @Valid EditAdminParam editAdminParam) {
        accountService.editAdmin(
                ParamUtil.parseId(editAdminParam.getAccountId()),
                editAdminParam.getUsername(),
                editAdminParam.getName(),
                editAdminParam.getPhoneNumber(),
                ParamUtil.parseId(editAdminParam.getAdminRoleId()));
        return Result.success(null);
    }

    @Transactional
    @Operation(summary = "设置管理员的禁用状态")
    @ApiImplicitParam(dataType = "BlockAdminParam", name = "blockAdminParam", value = "禁用管理员", readOnly = true, required = true)
    @PostMapping("/blockAdmin")
    @LogAnnotation(module = "AdminController", operation = "禁用管理员")
    @Permission(permission_code = PermissionCode.BLOCK_ADMIN,
            content = "设置管理员“{username}”的禁用状态为{isBlock}")
    public Result blockAdmin(@RequestBody @Valid BlockAdminParam blockAdminParam) {
        accountService.blockAdmin(
                nowUserGetter.getNowUser().getAccountId(),
                ParamUtil.parseId(blockAdminParam.getAccountId()),
                blockAdminParam.getIsBlock()
        );
        return Result.success(null);
    }

    @Transactional
    @Operation(summary = "删除管理员")
    @ApiImplicitParam(dataType = "DeleteAdminParam", name = "deleteAdminParam", value = "删除管理员", readOnly = true, required = true)
    @PostMapping("/deleteAdmin")
    @LogAnnotation(module = "AdminController", operation = "删除管理员")
    @Permission(permission_code = PermissionCode.DELETE_ADMIN,
            content = "删除管理员“{username}”")
    public Result deleteAdmin(@RequestBody @Valid DeleteAdminParam deleteAdminParam) {
        accountService.deleteAdmin(
                nowUserGetter.getNowUser().getAccountId(),
                ParamUtil.parseId(deleteAdminParam.getAccountId())
        );
        return Result.success(null);
    }

    @Transactional
    @Operation(summary = "修改其他管理员的密码")
    @ApiImplicitParam(dataType = "EditPasswordParam", name = "editPasswordParam", value = "删除管理员", readOnly = true, required = true)
    @PostMapping("/editPassword")
    @LogAnnotation(module = "AdminController", operation = "修改其他管理员的密码")
    @Permission(permission_code = PermissionCode.EDIT_ADMIN_PASSWORD,
            content = "修改管理员“{username}”的密码")
    public Result editPassword(@RequestBody @Valid EditPasswordParam editPasswordParam) {
        accountService.editPassword(
                nowUserGetter.getNowUser().getAccountId(),
                ParamUtil.parseId(editPasswordParam.getAccountId()),
                editPasswordParam.getPassword(),
                editPasswordParam.getRePassword()
        );
        return Result.success(null);
    }

    @Transactional
    @Operation(summary = "修改管理员的功能权限")
    @ApiImplicitParam(dataType = "EditFunctionPermissionParam", name = "editFunctionPermissionParam", value = "修改管理员的功能权限", readOnly = true, required = true)
    @PostMapping("/editFunctionPermission")
    @LogAnnotation(module = "AdminController", operation = "修改功能权限")
    @Permission(permission_code = PermissionCode.EDIT_ADMIN_FUNCTION_PERMISSION,
            content = "修改管理员“{username}”的功能权限")
    public Result editFunctionPermission(@RequestBody @Valid EditFunctionPermissionParam editFunctionPermissionParam) {
        accountService.editFunctionPermission(
                ParamUtil.parseId(editFunctionPermissionParam.getAccountId()),
                editFunctionPermissionParam.getFunctionPermissionList()
        );
        return Result.success(null);
    }

    @Transactional
    @Operation(summary = "修改管理员的单位权限")
    @ApiImplicitParam(dataType = "EditCompanyPermissionParam", name = "editCompanyPermissionParam", value = "修改管理员的单位权限", readOnly = true, required = true)
    @PostMapping("/editCompanyPermission")
    @LogAnnotation(module = "AdminController", operation = "修改单位权限")
    @Permission(permission_code = PermissionCode.EDIT_ADMIN_COMPANY_PERMISSION,
            content = "修改管理员“{username}”的单位权限")
    public Result editCompanyPermission(@RequestBody @Valid EditCompanyPermissionParam editCompanyPermissionParam) {
        accountService.editCompanyPermission(
                ParamUtil.parseId(editCompanyPermissionParam.getAccountId()),
                ParamUtil.parseIdList(editCompanyPermissionParam.getCompanyPermissionList())
        );
        return Result.success(null);
    }

    @Transactional
    @Operation(summary = "修改管理员的部门权限")
    @ApiImplicitParam(dataType = "EditDeptPermissionParam", name = "editDeptPermissionParam", value = "修改管理员的部门权限", readOnly = true, required = true)
    @PostMapping("/editDepartmentPermission")
    @LogAnnotation(module = "AdminController", operation = "修改部门权限")
    @Permission(permission_code = PermissionCode.EDIT_ADMIN_DEPT_PERMISSION,
            content = "修改管理员“{username}”的部门权限")
    public Result editDepartmentPermission(@RequestBody @Valid EditDeptPermissionParam editDeptPermissionParam) {
        accountService.editDepartmentPermission(
                ParamUtil.parseId(editDeptPermissionParam.getAccountId()),
                ParamUtil.parseIdList(editDeptPermissionParam.getDeptPermissionList())
        );
        return Result.success(null);
    }

    @Transactional
    @Operation(summary = "获得管理员操作日志")
    @ApiImplicitParam(dataType = "GetOperateLogParam", name = "getOperateLogParam", value = "查看操作日志", readOnly = true, required = true)
    @PostMapping("/getOperateLog")
    @LogAnnotation(module = "AdminController", operation = "查看操作日志")
    @Permission(permission_code = PermissionCode.SUPER_OPERATE_LOG)
    public Result<GetOperateLogVo> getOperateLog(@RequestBody @Valid GetOperateLogParam getOperateLogParam) {
        GetOperateLogVo result = accountService.getOperateLog(
                getOperateLogParam.getPage(),
                getOperateLogParam.getPageSize(),
                getOperateLogParam.getUsername(),
                getOperateLogParam.getName(),
                getOperateLogParam.getStartTime(),
                getOperateLogParam.getEndTime()
        );
        return Result.success(result);
    }

}
